const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const convertKeysToLowerCase = obj => {
  if (obj instanceof Array) {
    // 处理数组，只对数组内的对象或Map处理，不改变数组本身的值
    return obj.map(item => {
      if (item instanceof Object && !(item instanceof Map)) {
        return convertKeysToLowerCase(item);
      } else if (item instanceof Map) {
        let newMap = new Map();
        item.forEach((value, key) => {
          let newKey = key.charAt(0).toLowerCase() + key.slice(1);
          newMap.set(newKey, value instanceof Object ? convertKeysToLowerCase(value) : value);
        });
        return newMap;
      }
      return item;
    });
  } else if (obj instanceof Object && !(obj instanceof Map)) {
    // 处理普通对象
    return Object.keys(obj).reduce((result, key) => {
      let newKey = key.charAt(0).toLowerCase() + key.slice(1);
      result[newKey] = obj[key] instanceof Object ? convertKeysToLowerCase(obj[key]) : obj[key];
      return result;
    }, {});
  } else if (obj instanceof Map) {
    // 处理Map
    let newMap = new Map();
    obj.forEach((value, key) => {
      let newKey = key.charAt(0).toLowerCase() + key.slice(1);
      newMap.set(newKey, value instanceof Object ? convertKeysToLowerCase(value) : value);
    });
    return newMap;
  }
  return obj;
}

module.exports = {
  formatTime,
  convertKeysToLowerCase
}

